package com.road.yishi.mina;

import java.net.SocketAddress;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 网关转发到各个服务器之间数据包处理
 */
public class ServerHandler extends IoHandlerAdapter {

	private static final Logger Log = LoggerFactory.getLogger(ServerHandler.class);

	private String handlerName;

	public ServerHandler(String handlerName) {
		this.handlerName = handlerName;
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		Log.info(toMessage(" recieved a gateway connector from " + session.getRemoteAddress()));
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		System.out.println(toMessage("recieved a gateway message , " + message));
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		System.out.println("sessionClosed");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		SocketAddress socketAddress = session.getRemoteAddress();
		String address = (socketAddress != null) ? socketAddress.toString() : "";
		Log.error(toMessage("caught exception that close the connection to gateway disconnected. session : " + address), cause);
	}

	// @Override
	// public void messageReceived(IoSession session, Object message) throws
	// Exception {
	// try {
	// PBMessage packet = (PBMessage) message;
	// short code = packet.getCode();
	// Command cmd = CommandSet.getCommand(code);
	// if (cmd == null) {
	// Log.warn(toMessage(" Can not found code = " + code + ",drop this
	// packet."));
	// return;
	// }
	// cmd.execute(session, packet);
	// } catch (Exception e) {
	// Log.error(toMessage("Handler execute has exception:"), e);
	// }
	// }
	//
	// @Override
	// public void sessionClosed(IoSession session) throws Exception {
	// if (session.getAttribute(LinkedClient.KEY_CLIENT) != null) {
	// Log.info(toMessage("close the connection to gateway disconnected. session
	// : " + session.getRemoteAddress()));
	// LinkedClient client = (LinkedClient)
	// session.getAttribute(LinkedClient.KEY_CLIENT);
	// client.disConnect();
	// GatewayLinkedSet.removeLinkedClient(session);
	// }
	// }
	//
	// @Override
	// public void exceptionCaught(IoSession session, Throwable cause) throws
	// Exception {
	// SocketAddress socketAddress = session.getRemoteAddress();
	// String address = (socketAddress != null) ? socketAddress.toString() : "";
	// session.close(true);
	// if (cause != null) {
	// Log.error(toMessage("caught exception that close the connection to
	// gateway disconnected. session : " + address), cause);
	// }
	//
	// if (session.getAttribute(LinkedClient.KEY_CLIENT) != null) {
	// LinkedClient client = (LinkedClient)
	// session.getAttribute(LinkedClient.KEY_CLIENT);
	// GatewayLinkedSet.removeLinkedClient(session);
	// Log.info(toMessage("close the connection to gateway disconnected. session
	// : " + address));
	//
	// client.disConnect();
	// }
	// }

	protected String toMessage(String msg) {
		return "[" + handlerName + "] " + msg;
	}
}
